/**
 *
 */
package com.china.lee.fileoperate;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;

/**
 * @author li-l
 */
public class fileOperate {
	/**
	 * Copy File
	 * 
	 * @param oldPath String c:/fqf.txt
	 * @param newPath String f:/fqf.txt
	 */
	public static void copyFile(String oldPath, String newPath) {
		try {
			oldPath = correctPath(oldPath, false);
			newPath = correctPath(newPath, false);
			int bytesum = 0;
			int byteread = 0;
			File oldfile = new File(oldPath);
			if (oldfile.exists()) { // File Exists
				InputStream inStream = new FileInputStream(oldPath); // Read old File
				FileOutputStream fs = new FileOutputStream(newPath);
				byte[] buffer = new byte[1444];
				while ((byteread = inStream.read(buffer)) != -1) {
					bytesum += byteread; // File size
					// System.out.println(bytesum);
					fs.write(buffer, 0, byteread);
				}
				inStream.close();
			}
		} catch (Exception e) {
			System.out.println("Copy File Err");
			e.printStackTrace();
		}
	}

	/**
	 * Copy File Batch
	 * 
	 * @param oldPath String c:/fqf.txt
	 * @param newPath String f:/fqf.txt
	 */
	public static void copyFileBatch(String oldPath, String newPath) {
		try {
			oldPath = correctPath(oldPath, true);
			newPath = correctPath(newPath, true);
			exeBatch("CMD /C copy /y "
					+ oldPath
					+ " "
					+ newPath);
		} catch (Exception e) {
			System.out.println("Copy File Err");
			e.printStackTrace();
		}
	}

	/**
	 * Copy Folder
	 * 
	 * @param oldPath String c:/fqf
	 * @param newPath String f:/fqf/ff
	 */
	public static void copyFolder(String oldPath, String newPath) {

		try {
			oldPath = correctPath(oldPath, false);
			newPath = correctPath(newPath, false);
			(new File(newPath)).mkdirs(); // If folder not exist, create new folder
			File a = new File(oldPath);
			String[] file = a.list();
			File temp = null;
			for (String element : file) {
				if (oldPath.endsWith(File.separator)) {
					temp = new File(oldPath
							+ element);
				} else {
					temp = new File(oldPath
							+ File.separator
							+ element);
				}

				if (temp.isFile()) {
					FileInputStream input = new FileInputStream(temp);
					FileOutputStream output = new FileOutputStream(newPath
							+ "/"
							+ (temp.getName()).toString());
					byte[] b = new byte[1024 * 5];
					int len;
					while ((len = input.read(b)) != -1) {
						output.write(b, 0, len);
					}
					output.flush();
					output.close();
					input.close();
				}
				if (temp.isDirectory()) {// sub folder
					copyFolder(oldPath
							+ "/"
							+ element, newPath
							+ "/"
							+ element);
				}
			}
		} catch (Exception e) {
			System.out.println("Copy Folder Err");
			e.printStackTrace();
		}
	}

	/**
	 * Copy Folder Batch
	 * 
	 * @param oldPath String c:/fqf
	 * @param newPath String f:/fqf
	 */
	public static void copyFolderBatch(String oldPath, String newPath) {
		try {
			oldPath = correctPath(oldPath, true);
			newPath = correctPath(newPath, true);
			exeBatch("CMD /C xcopy /y /i /k /e /r /h "
					+ oldPath
					+ " "
					+ newPath);
		} catch (Exception e) {
			System.out.println("Copy File Err");
			e.printStackTrace();
		}
	}

	/**
	 * Path.
	 * 
	 * @param path the path
	 * @param blank the blank
	 * @return path
	 */
	public static String correctPath(String path, boolean blank) {
		String ret_path = path.toString();
		ret_path = ret_path.replace("/", "\\");
		ret_path = ret_path.replace("/", "\\");
		ret_path = ret_path.replace("\\\\", "\\");
		if (ret_path.contains(" ")
				&& blank) {
			ret_path = "\""
					+ ret_path
					+ "\"";
		}
		return ret_path;
	}

	/**
	 * Delete All File And Sub Folder in Folder Batch
	 * 
	 * @param path String c:/fqf
	 * @param keep curren path
	 */
	public static void delAllBatch(String path, boolean keep) {
		try {
			boolean tmp = true;
			String filePath = correctPath(path, true);
			java.io.File myFilePath = new java.io.File(filePath);
			if (!myFilePath.exists()) {
				tmp = false;
			}

			exeBatch("CMD /C rd /s /q "
					+ filePath);
			if (keep
					&& tmp) {
				exeBatch("CMD /C md "
						+ filePath);
			}
		} catch (Exception e) {
			System.out.println("Delete AllFile Err");
			e.printStackTrace();
		}
	}

	/**
	 * Delete All File in Folder
	 * 
	 * @param path String c:/fqf
	 */
	public static void delAllFile(String path) {
		path = correctPath(path, false);
		File file = new File(path);
		if (!file.exists()) {
			return;
		}
		if (!file.isDirectory()) {
			return;
		}
		String[] tempList = file.list();
		File temp = null;
		for (String element : tempList) {
			if (path.endsWith(File.separator)) {
				temp = new File(path
						+ element);
			} else {
				temp = new File(path
						+ File.separator
						+ element);
			}
			if (temp.isFile()) {
				temp.delete();
			}
			if (temp.isDirectory()) {
				delAllFile(path
						+ "/"
						+ element);// Delete All File
				delFolder(path
						+ "/"
						+ element);// Delete Foleder
			}
		}
	}

	/**
	 * DeleteFile
	 * 
	 * @param filePathAndName String c:/fqf.txt
	 */
	public static void delFile(String filePathAndName) {
		try {
			String filePath = correctPath(filePathAndName, false);
			java.io.File myDelFile = new java.io.File(filePath);
			myDelFile.delete();

		} catch (Exception e) {
			System.out.println("Delete File Err");
			e.printStackTrace();
		}
	}

	/**
	 * DeleteFile
	 * 
	 * @param filePathAndName String c:/fqf.txt
	 */
	public static void delFileBatch(String filePathAndName) {
		try {
			String filePath = correctPath(filePathAndName, true);
			exeBatch("CMD /C del /a /f /q "
					+ filePath);

		} catch (Exception e) {
			System.out.println("Delete File Err");
			e.printStackTrace();
		}
	}

	/**
	 * DeleteFolder
	 * 
	 * @param folderPath String c:/fqf
	 */
	public static void delFolder(String folderPath) {
		try {
			folderPath = correctPath(folderPath, false);
			delAllFile(folderPath); // Delete All File
			java.io.File myFilePath = new java.io.File(folderPath);
			myFilePath.delete(); // Delete Folder

		} catch (Exception e) {
			System.out.println("Delete Folder Err");
			e.printStackTrace();
		}
	}

	/**
	 * DeleteFolderBatch
	 * 
	 * @param folderPath String c:/fqf
	 */
	public static void delFolderBatch(String folderPath) {
		try {
			String filePath = correctPath(folderPath, true);
			exeBatch("CMD /C rd /s /q "
					+ filePath);

		} catch (Exception e) {
			System.out.println("Delete Folder Err");
			e.printStackTrace();
		}
	}

	/**
	 * Execute Batch Command
	 * 
	 * @param cmd String
	 */
	public static void exeBatch(String cmd) {
		try {
			Runtime exe = Runtime.getRuntime();
			Process process = exe.exec(cmd);

			InputStream is = process.getInputStream();
			InputStreamReader bi = new InputStreamReader(is);
			BufferedReader br = new BufferedReader(bi);
			String message = br.readLine();
			while ((message != null)
					&& !"".equals(message)) {
				System.out.println(message);
				message = br.readLine();
			}
		} catch (Exception e) {
			System.out.println("Exe Batch Err");
			System.out.println("Batch Command is : "
					+ cmd);
			e.printStackTrace();
		}
	}

	/**
	 * Move File
	 * 
	 * @param oldPath String c:/fqf.txt
	 * @param newPath String d:/fqf.txt
	 */
	public static void moveFile(String oldPath, String newPath) {
		oldPath = correctPath(oldPath, false);
		newPath = correctPath(newPath, false);
		copyFile(oldPath, newPath);
		delFile(oldPath);
	}

	/**
	 * Move File And Folder Batch
	 * 
	 * @param oldPath String c:/fqf
	 * @param newPath String d:/fqf
	 */
	public static void moveFileBatch(String oldPath, String newPath) {
		try {
			oldPath = correctPath(oldPath, true);
			newPath = correctPath(newPath, true);
			exeBatch("CMD /C move /y "
					+ oldPath
					+ " "
					+ newPath);
		} catch (Exception e) {
			System.out.println("Move File Err");
			e.printStackTrace();
		}
	}

	/**
	 * Move Folder
	 * 
	 * @param oldPath String c:/fqf.txt
	 * @param newPath String d:/fqf.txt
	 */
	public static void moveFolder(String oldPath, String newPath) {
		oldPath = correctPath(oldPath, false);
		newPath = correctPath(newPath, false);
		copyFolder(oldPath, newPath);
		delFolder(oldPath);
	}

	/**
	 * CreateFile
	 * 
	 * @param filePathAndName String c:/fqf.txt
	 * @param fileContent String
	 */
	public static void newFile(String filePathAndName, String fileContent) {

		try {
			String filePath = correctPath(filePathAndName, false);
			File myFilePath = new File(filePath);
			if (!myFilePath.exists()) {
				myFilePath.createNewFile();
			}
			FileWriter resultFile = new FileWriter(myFilePath);
			PrintWriter myFile = new PrintWriter(resultFile);
			String strContent = fileContent;
			myFile.println(strContent);
			resultFile.close();

		} catch (Exception e) {
			System.out.println("Create File Err");
			e.printStackTrace();
		}
	}

	/**
	 * CreateFileBatch
	 * 
	 * @param filePathAndName String c:/fqf.txt
	 * @param fileContent String
	 */
	public static void newFileBatch(String filePathAndName, String fileContent) {

		try {
			String filePath = correctPath(filePathAndName, true);
			exeBatch("CMD /C echo "
					+ fileContent
					+ ">"
					+ filePath);
		} catch (Exception e) {
			System.out.println("Create File Err");
			e.printStackTrace();
		}
	}

	/**
	 * Create Folder
	 * 
	 * @param folderPath String c:/fqf
	 */
	public static void newFolder(String folderPath) {
		try {
			String filePath = correctPath(folderPath, false);
			java.io.File myFilePath = new java.io.File(filePath);
			if (!myFilePath.exists()) {
				myFilePath.mkdir();
			}
		} catch (Exception e) {
			System.out.println("Create Folder Err");
			e.printStackTrace();
		}
	}

	/**
	 * Create Folder Using Batch
	 * 
	 * @param folderPath String c:/fqf
	 */
	public static void newFolderBatch(String folderPath) {
		try {
			String filePath = correctPath(folderPath, true);
			exeBatch("CMD /C MD "
					+ filePath);
		} catch (Exception e) {
			System.out.println("Create Folder Err");
			e.printStackTrace();
		}
	}
}
